
// Copyright (C) Recursion Company. All rights reserved.

#include "stdafx.h"
#include "vol_user_app_info.h"

//-----------------------------------------------------------------------------------------

CVWnd_ScrollBar::CVWnd_ScrollBar ()
{
    m_npInitialMin = 1;
    m_npInitialMax = 100;
    m_npInitialPos = 1;
    m_npPageStep = 10;
    m_npLineStep = 1;
    m_blpAllowTrack = TRUE;
}

BOOL CVWnd_ScrollBar::CreateControl (CVolNativeWndCreateParam* pCreateParam)
{
    if (_CREATE_WND_BY_PARAM (pCreateParam) != NULL)
    {
        const INT nInitialMin = (INT)MIN (m_npInitialMin, m_npInitialMax);
        const INT nInitialMax = (INT)MAX (m_npInitialMin, m_npInitialMax);

        SetScrollRange (nInitialMin, nInitialMax);
        SetScrollPos (CLIP ((INT)m_npInitialPos, nInitialMin, nInitialMax));
        
        return TRUE;
    }

    return FALSE;
}

void CVWnd_ScrollBar::OnReflectedHScroll (UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
{
    OnScroll (nSBCode, nPos);
}

void CVWnd_ScrollBar::OnReflectedVScroll (UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
{
    OnScroll (nSBCode, nPos);
}

void CVWnd_ScrollBar::OnScroll (UINT nSBCode, UINT nPos) 
{
    if (nSBCode == SB_ENDSCROLL)
        return;

    INT nOldPos = GetScrollPos ();

    INT nMinPos = 0, nMaxPos = 0;
    GetScrollRange (&nMinPos, &nMaxPos);

    INT nNewPos = nOldPos;
    switch (nSBCode)
    {
    case SB_LEFT:
    // case SB_TOP:
        COMPILE_TIME_ASSERT (SB_LEFT == SB_TOP);
        nNewPos = nMinPos;
        break;

    case SB_RIGHT:
    // case SB_BOTTOM:
        COMPILE_TIME_ASSERT (SB_RIGHT == SB_BOTTOM);
        nNewPos = nMaxPos;
        break;

    case SB_LINELEFT:
    // case SB_LINEUP:
        COMPILE_TIME_ASSERT (SB_LINELEFT == SB_LINEUP);
        nNewPos -= (INT)m_npLineStep;
        break;

    case SB_LINERIGHT:
    // case SB_LINEDOWN:
        COMPILE_TIME_ASSERT (SB_LINERIGHT == SB_LINEDOWN);
        nNewPos += (INT)m_npLineStep;
        break;

    case SB_PAGELEFT:
    // case SB_PAGEUP:
        COMPILE_TIME_ASSERT (SB_PAGELEFT == SB_PAGEUP);
        nNewPos -= (INT)m_npPageStep;
        break;

    case SB_PAGERIGHT:
    // case SB_PAGEDOWN:
        COMPILE_TIME_ASSERT (SB_PAGERIGHT == SB_PAGEDOWN);
        nNewPos += (INT)m_npPageStep;
        break;

    case SB_THUMBTRACK:
        if (m_blpAllowTrack == FALSE)
            return;
        nNewPos = nPos;
        break;

    case SB_THUMBPOSITION:
        nNewPos = nPos;
        break;
    }

    nNewPos = CLIP (nNewPos, nMinPos, nMaxPos);

    if (nNewPos != nOldPos)
    {
        SetScrollPos (nNewPos);
        
        if (m_pEventListener != NULL)
            m_pEventListener->OnScroll ();
    }
}
